﻿using System;
using System.Data;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LetsDoIt.Utils
{
    public class DESCrypt
    {
        static byte[] somebytes = ASCIIEncoding.ASCII.GetBytes("L" + "e" + "t" + "s" + "D" +"o"+"I"+"t");
        /// <summary>
        /// Encrypt a string.
        /// </summary>
        /// <param name="originalString">The original string.</param>
        /// <returns>The encrypted string.</returns>
        /// <exception cref="ArgumentNullException">This exception will be 
        /// thrown when the original string is null or empty.</exception>
        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                throw new NoNullAllowedException("The string which needs to be encrypted can not be null.");
            }
            try
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(somebytes, somebytes), CryptoStreamMode.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(cryptoStream))
                        {
                            writer.Write(originalString);
                            writer.Flush();
                            cryptoStream.FlushFinalBlock();
                            writer.Flush();
                            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Program.Log.Error(ex.ToString());
            }
            return string.Empty;
        }

        /// <summary>
        /// Decrypt a crypted string.
        /// </summary>
        /// <param name="cryptedString">The crypted string.</param>
        /// <returns>The decrypted string.</returns>
        /// <exception cref="ArgumentNullException">This exception will be thrown 
        /// when the crypted string is null or empty.</exception>
        public static string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new NoNullAllowedException("The string which needs to be decrypted can not be null.");
            }
            try
            {
                using (DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider())
                {
                    using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString)))
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(somebytes, somebytes), CryptoStreamMode.Read))
                        {
                            using (StreamReader reader = new StreamReader(cryptoStream))
                            {
                                return reader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Program.Log.Error(ex.ToString());
            }
            return string.Empty;
        }
    }
}
